<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="物料编号" prop="matNo">
          <el-input
            v-model="queryParams.matNo"
            placeholder="请输入物料编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="matName">
          <el-input
            v-model="queryParams.matName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="颜色" prop="clName">
          <el-input
            v-model="queryParams.clName"
            placeholder="请输入颜色"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户产品名称" prop="custMatName">
          <el-input
            v-model="queryParams.custMatName"
            placeholder="请输入客户产品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="币种" prop="curId">
          <el-select v-model="queryParams.invoice" placeholder="请选择币种" clearable size="small">
              <el-option v-for="dict in dict.type.sys_currency" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="英文名称" prop="engName">
          <el-input
            v-model="queryParams.engName"
            placeholder="请输入英文名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="英文颜色" prop="engColor">
          <el-input
            v-model="queryParams.engColor"
            placeholder="请输入英文颜色"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        
        <el-form-item label="物料分类" prop="mcId">
          <el-select v-model="queryParams.mcId" placeholder="请选择物料分类" clearable size="small">
              <el-option v-for="dict in dict.type.sys_mc_name" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:material:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:material:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:material:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="物料编号" width="120" align="center" prop="matNo" />
        <el-table-column label="物料名称" width="120" align="center" prop="matName" />
        <el-table-column label="规格" align="center" prop="type" />
        <el-table-column label="颜色" align="center" prop="clName" />
        <el-table-column label="客户产品名称" width="120" align="center" prop="custMatName" />
        <el-table-column label="币种" align="center" prop="curId">
          <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_currency" :value="scope.row.curId"/>
            </template>
        </el-table-column>
        <el-table-column label="英文名称" align="center" prop="engName" />
        <el-table-column label="英文颜色" align="center" prop="engColor" />
        <el-table-column label="图片" align="center" prop="imagePath" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.imagePath" :width="50" :height="50"/>
          </template>
        </el-table-column>
        
        <el-table-column label="创建人名称" width="120" align="center" prop="createStaffName" />
        <el-table-column label="创建日期" align="center" prop="createDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
       
        <el-table-column label="修改人名称" width="120" align="center" prop="modifyStaffName" />
        <el-table-column label="修改日期" align="center" prop="modifyDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.modifyDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核日期" align="center" prop="passDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.passDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        
        <el-table-column label="审核人名称" width="120" align="center" prop="passStaffName" />
        <el-table-column label="审核状态" align="center" prop="pass">
          <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_pass_status" :value="scope.row.pass"/>
            </template>
        </el-table-column>
        <el-table-column label="物料分类" align="center" prop="mcId">
          <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_mc_name" :value="scope.row.mcId"/>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:material:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-share"
              @click="handlePass(scope.row)"
            >{{ scope.row.pass == 1?'反审':'审核' }}</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:material:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改物料信息对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="物料编号" prop="matNo">
            <el-input v-model="form.matNo" placeholder="请输入物料编号" />
          </el-form-item>
          <el-form-item label="物料名称" prop="matName">
            <el-input v-model="form.matName" placeholder="请输入物料名称" />
          </el-form-item>
          <el-form-item label="颜色" prop="clName">
            <el-input v-model="form.clName" placeholder="请输入颜色" />
          </el-form-item>
          <el-form-item label="客户产品名称" prop="custMatName">
            <el-input v-model="form.custMatName" placeholder="请输入客户产品名称" />
          </el-form-item>
          <el-form-item label="币种" prop="curId">
            <el-select v-model="form.curId" placeholder="请选择币种">
            <el-option
              v-for="dict in dict.type.sys_currency"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="英文名称" prop="engName">
            <el-input v-model="form.engName" placeholder="请输入英文名称" />
          </el-form-item>
          <el-form-item label="英文颜色" prop="engColor">
            <el-input v-model="form.engColor" placeholder="请输入英文颜色" />
          </el-form-item>
          <el-form-item label="图片" prop="imagePath">
            <image-upload v-model="form.imagePath"/>
          </el-form-item>
          
         
          <el-form-item label="物料分类" prop="mcId">
            <el-select v-model="form.mcId" placeholder="请输入物料分类" clearable size="small">
              <el-option v-for="dict in dict.type.sys_mc_name" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="规格" prop="type">
            <el-input v-model="form.type" placeholder="请输入规格" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial,passMaterial } from "@/api/system/material/material"
  
  export default {
    name: "Material",
    dicts: ['sys_mc_name', 'sys_currency','sys_pass_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 物料信息表格数据
        materialList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          matNo: null,
          matName: null,
          type: null,
          clName: null,
          custMatName: null,
          curId: null,
          engName: null,
          engColor: null,
          imagePath: null,
          createStaffId: null,
          createStaffName: null,
          createDate: null,
          modifyStaffId: null,
          modifyStaffName: null,
          modifyDate: null,
          passDate: null,
          passStaffId: null,
          passStaffName: null,
          pass: null,
          mcId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询物料信息列表 */
      getList() {
        this.loading = true
        listMaterial(this.queryParams).then(response => {
          this.materialList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          matId: null,
          matNo: null,
          matName: null,
          type: null,
          clName: null,
          custMatName: null,
          curId: null,
          engName: null,
          engColor: null,
          imagePath: null,
          createStaffId: null,
          createStaffName: null,
          createDate: null,
          modifyStaffId: null,
          modifyStaffName: null,
          modifyDate: null,
          passDate: null,
          passStaffId: null,
          passStaffName: null,
          pass: null,
          mcId: null
        }
        this.resetForm("form")
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.matId)
        this.single = selection.length==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "添加物料信息"
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const matId = row.matId || this.ids
        getMaterial(matId).then(response => {
          this.form = response.data
          this.open = true
          this.title = "修改物料信息"
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.matId != null) {
              updateMaterial(this.form).then(response => {
                this.$modal.msgSuccess("修改成功")
                this.open = false
                this.getList()
              })
            } else {
              addMaterial(this.form).then(response => {
                this.$modal.msgSuccess("新增成功")
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const matIds = row.matId || this.ids
        this.$modal.confirm('是否确认删除物料信息编号为"' + matIds + '"的数据项？').then(function() {
          return delMaterial(matIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/material/export', {
          ...this.queryParams
        }, `material_${new Date().getTime()}.xlsx`)
      },
      handlePass(row){
        const matId = row.matId
        passMaterial(matId).then(response =>{
          this.getList()
        })
      }
    }
  }
  </script>
  